package org.iabako.client.ui.form.business;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.InputText;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.BusinessEntity;
import org.iabako.shared.tools.GenericTools;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by jose on 16/02/15.
 */
public class AddLinkForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, AddLinkForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    @UiField
    Button save;
    @UiField
    InputText linkUrl;
    @UiField
    InputText linkName;

    public AddLinkForm() {
        simplePopup = true;
        setForm(binderForm, PageConfigEnum.Login, null);
    }

    @Override
    protected List<IField> getMandatoryFields() {
        if (!GenericTools.isEmpty(linkName.getValue())) {
            return Arrays.asList((IField) linkUrl);
        }
        if (!GenericTools.isEmpty(linkUrl.getValue())) {
            return Arrays.asList((IField) linkName);
        }
        return new ArrayList<IField>();
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        if (!uiValidation()) {
            return false;
        }

        if (entity instanceof BusinessEntity) {
            ((BusinessEntity) entity).setExternalLinkName(linkName.getValue());
            ((BusinessEntity) entity).setExternalLinkUrl(linkUrl.getValue());
            return true;
        }

        return false;
    }

    public void setEntity(BusinessEntity e) {
        this.entity = e;

        if (!GenericTools.isEmpty(((BusinessEntity) parentForm.entity).getExternalLinkName())) {
            linkName.setValue(((BusinessEntity) parentForm.entity).getExternalLinkName());
        }

        if (!GenericTools.isEmpty(((BusinessEntity) parentForm.entity).getExternalLinkUrl())) {
            linkUrl.setValue(((BusinessEntity) parentForm.entity).getExternalLinkUrl());
        }

        save.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (!validate()) {
                    return;
                }

                BusinessEntity businessEntity = (BusinessEntity) entity;

                parentForm.businessEntityService.saveExternalLinkInfo(
                        businessEntity, businessEntity.getExternalLinkName(), businessEntity.getExternalLinkUrl(),
                        new IabakoAsyncCallback<Void>() {
                            public void onSuccess(Void result) {
                                super.onSuccess(result);

                                parentForm.refreshLinkPanel();

                                popup.timerAfterClosePopupAnimation = new Timer() {
                                    public void run() {
                                        NavigationController.getInstance().closePopup(simplePopup);
                                        ValidationMessage.getFirstFromDOM().showSuccessMessage("validation_add_link_success");
                                    }
                                };
                                popup.handleClickClose();
                            }
                        });
            }
        });
    }
}
